package com.zhq.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

/**
 * @Author:钟豪强
 * @Description:    短信验证码
 * @Date Created in  2020-08-13 20:45
 * @Modified By:
 */
public class SMSUtil {

    public static void main(String[] args) {
        Random random = new Random();
        int num = random.nextInt(900000)+100000;//899999+100000的随机数
        send("18578410852",num+"");



    }

    /**
     * 发取件码
     * @param phoneNumber   收件人电话号码
     * @param code  取件码
     * @return  true代表发送成功
     */
    public static boolean send(String phoneNumber,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GD69EeNKnAsTr4arsDq", "3NE5hSLFmE7EYRYnt9HS0VUyoyJss4");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "快递e栈");
        request.putQueryParameter("TemplateCode", "SMS_199795811");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String json = response.getData();
            Gson g = new Gson();
            HashMap result = g.fromJson(json, HashMap.class);
            if("OK".equals(result.get("Message"))) {
                return true;
            }else{
                System.out.println("短信发送失败，原因："+result.get("Message"));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 发送登录验证码
     * @param phoneNumber   登录的手机号码
     * @param code          验证码
     * @return  true代表发送成功
     */
    public static boolean loginSMS(String phoneNumber,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GD69EeNKnAsTr4arsDq", "3NE5hSLFmE7EYRYnt9HS0VUyoyJss4");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "快递e栈");
        request.putQueryParameter("TemplateCode", "SMS_200185271");//SMS_200185271
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String json = response.getData();
            Gson g = new Gson();
            HashMap result = g.fromJson(json, HashMap.class);
            if("OK".equals(result.get("Message"))) {
                return true;
            }else{
                System.out.println("短信发送失败，原因："+result.get("Message"));
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 短信延时发送
     */
    public static void sendDelay(Map<Long, String> map) {
        // System.out.println("hahah ");
        LocalDateTime localDateTime = LocalDateTime.now();
        Long second = localDateTime.minusMinutes(1).toEpochSecond(ZoneOffset.of("+8"));
        // System.out.println("kkk" + second);
        long l = localDateTime.plusMinutes(8).toEpochSecond(ZoneOffset.of("+8"));
        // System.out.println("lala" + l);
        for (Long i = second; i < l; i++) {
            // System.out.println("所有的i" + i);
            if (map.containsKey(i)) {
                // System.out.println("hahahh"); //1598083690  1598083690
                //录入成功
                String phoneAndCode = map.get(i);
                // System.out.println(phoneAndCode);
                if (phoneAndCode != null) {
                    String[] split = phoneAndCode.split(",");
                    if (split[0] != null && split[1] != null) {
                        SMSUtil.send(split[0],split[1]);
                    }
                }

            }
        }
    }

}
